package com.anosine.gpsl.android.util.ormdb;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.anosine.gpsl.android.entity.PushMessageBean;
import com.anosine.gpsl.android.util.MyLog;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import java.sql.SQLException;
/**
 * @Title: TabMessageFragment
 * @Package com.anosine.gpsl.android.ui.fragment
 * @Description: 报警消息界面
 * @author: sunny
 * @date: 2015-11-23
 * @version V1.0
 */

//地数据库存储,创建数据库的类
public class SqliteOpenHelper extends OrmLiteSqliteOpenHelper {
	private final String TAG = this.getClass().getSimpleName();
	private static SqliteOpenHelper instance;

	/* 在此声明实体类 */
	private Class<?>[] classes = { PushMessageBean.class };

	public SqliteOpenHelper(Context context) {
		super(context, DBConfig.getFilePath(), null, DBConfig.DATABASE.VERSION);
	}
//这个方法通常用来创建表,和初始数据的
	@Override
	public void onCreate(SQLiteDatabase database,
			ConnectionSource connectionSource) {
		try {
			//?表示通配符，即声明任意类的类型
			//clazz表示不定参数个数（类型也可不同）
			//Class类是个泛型，这里使用通配符?作为泛型实参表示classes这些对象可以接受任何类型（类类型）
			for (Class<?> clazz : classes) {
				//Create a table if it does not already exist. This is not supported by all databases
				TableUtils.createTableIfNotExists(connectionSource, clazz);
			}
		} catch (SQLException e) {
			MyLog.i(TAG, " 建立数据库失败！" + e.toString());
		}

	}
	//当数据库版本号更新时执行的方法 
	@Override
	public void onUpgrade(SQLiteDatabase database,
			ConnectionSource connectionSource, int oldVer, int newVer) {
		try {
			for (Class<?> clazz : classes) {
				//Issue the database statements to drop the table associated with a class
				TableUtils.dropTable(connectionSource, clazz, true);
			}
			//初始化数据
			onCreate(database, connectionSource);
		} catch (SQLException e) {
			MyLog.i(TAG, "Unable to upgrade database from version " + oldVer
                    + " to new " + newVer);
		}
	}

}
